import QS from '../miniprogram_npm/qs/index'
var app = getApp()
const baseUrl = app.globalData.apihost
import {
    rushToken
} from '../api/user'

const whiteUrl = [
    '/user/web/live/expire/hint',
    '/live/web/live/list',
    '/spcolumn/web/dict/getConstants/market',
]

export function request(method, url, data, file) {
    const token = wx.getStorageSync('token') || null
    const user = wx.getStorageSync('user')
    if (url != '/user/web/wxMini/login') {
        data.userId = user ? user.userId : ''
    }

    return new Promise((resolve, reject) => {
        let header = {
            'content-type': url == '/user/web/uploadImg' ? 'multipart/form-data' + ';' + 'boundary=' + sBoundary : 'application/x-www-form-urlencoded',
            'g-token': token,
        }
        var sBoundary = "---------------------------" + Date.now().toString(16)
        wx.request({
            url: baseUrl + url,
            method: method,
            header: header,
            data: method === 'POST' ? QS.stringify(data) : data,
            responseType: file ? "arraybuffer" : 'text',
            success: (res) => {
                console.log(res.data, res.data.status, '后台结果')
                if (file) {
                    let url = 'data:image/png;base64,' + wx.arrayBufferToBase64(res.data)
                    console.log(url, 'file----------------')
                    resolve(url);
                    return
                }
                if (res && res.data && res.data.status === 0) {
                    if (res.data.content) {


                        resolve(res.data);
                    } else {
                        if (url != '/user/web/get/coupon/show') {
                            wx.showToast({
                                title: res.data.msg,
                                icon: 'none'
                            })
                        }
                    }
                    resolve(res.data.content);
                } else if ([6001, 6002].includes(res.data.status)) {
                    wx.showToast({
                        title: res.data.msg,
                        icon: 'error'
                    })
                    wx.removeStorage({
                        key: 'token',
                    })
                    wx.removeStorage({
                        key: 'code',
                    })
                    wx.removeStorage({
                        key: 'user',
                    })
                    wx.switchTab({
                        url: '/pages/my/my',
                    })
                } else {

                    wx.showToast({
                        title: res.data.msg,
                        icon: 'none'
                    })
                    resolve(res.data.msg);
                }
            },
            fail: (err) => {
                console.log(err, '请求错误')
                wx.showToast({
                    title: err.errMsg,
                    icon: 'error'
                })
                reject(err);
            }
        })
    }).catch(err => {
        console.log(err, '--------------')
        wx.showToast({
            title: err.errMsg,
            icon: 'error'
        })
    })
}